package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.util.LoginAuthUtil;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.manager.service.ValidateCodeService;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.MenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syh
 * @create 2023-08-12 16:55
 */
@RestController
@RequestMapping(value = "/admin/system/index")
@Tag(name = "登录模块管理")
//@CrossOrigin(allowCredentials = "true" , originPatterns = "*" , allowedHeaders = "*")
public class IndexController {

    @Autowired
    private SysUserService sysUserService ;
    @Resource
    SysMenuService sysMenuService;
    @Resource
    ValidateCodeService validateCodeService;
    @Operation(summary = "查询登录用户分配的菜单列表")
    @GetMapping("menus")
    public Result<List<MenuVo>> listMenus(){
            List<MenuVo> menuVos=sysMenuService.listMenusByLoginUserId();
            return Result.build(menuVos,ResultCodeEnum.SUCCESS);

    }
    //登录功能
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto) ;
        return Result.build(loginVo , ResultCodeEnum.SUCCESS) ;
    }
    //验证码
    @GetMapping("/genValidateCode")
    public Result<ValidateCodeVo> genValidateCode(){
        ValidateCodeVo codeVo = validateCodeService.genValidateCode();
        return Result.build(codeVo,ResultCodeEnum.SUCCESS);
    }
    //获取用户信息
    @GetMapping("/userInfo")
    public Result<SysUser> userInfo(@RequestHeader("token") String token){
        //SysUser sysUser = sysUserService.getUserInfo(token);
//        SysUser sysUser = LoginAuthInterceptor.threadLocal.get();
        SysUser sysUser = LoginAuthUtil.get();
        return Result.build(sysUser,ResultCodeEnum.SUCCESS );
    }
    //注销用户
    @PostMapping("logout")
    public Result<Object> logout(@RequestHeader("token") String token){
        sysUserService.logout(token);
       // LoginAuthUtil.remove();
        return Result.build(null,ResultCodeEnum.SUCCESS );
    }


}
